<?php
include('../Lib/php/conex.php');
$link= conectaSQL("influenza","inf123456","influenza");
include('../Lib/php/fechas.php');
include('../Lib/php/funciones.php');
import_request_variables("g,P");
$imp_tabla = "<table width='750' border='0' align='center' cellpadding='3' cellspacing='3' class='datos cuadro2'><tr><td><img src='images/modi.png'  width='30' height='30' border='0' />";
$imp_fin_tabla = "</td></tr></table>";


if($nTipoMuestra == 0){
		$msg = "ESCRIBA UN TIPO DE PROCEDIMIENTO";
		echo $imp_tabla.$msg.$imp_fin_tabla;
		die();
}

if (!$nTipoMuestra > 0) {
	$accion = 1;	
	// Construye nombres de campos 
	$insert = "INSERT INTO $tabla (";
	$valores = " VALUES (";
	$query = mssql_query("select top 1 * from $tabla",$link);
	while($field = mssql_fetch_field($query)){
		$cCampo = $field->name;
		if($field->type == "char" or $field->type == "datetime") $com = "'"; else $com = "";
		foreach($_POST as $valor => $x){
			if($cCampo == $valor and $_POST[$cCampo] != ''){
				$insert .= $cCampo.",";
				if($field->type == "datetime")
					$valores .= $com.formatofecha($_POST[$cCampo])."$com,";
				else
					$valores .= $com.$_POST[$cCampo]."$com,";
			}
		}
	}
		
	$insert = substr($insert,0,-1);	
	$valores = substr($valores,0,-1);	
	$insert .= ")";
	$valores .= ")";
	$sql = $insert.$valores;

}else{
	$accion = 2;
	if($procedimiento == 1){
	$activo = 0;
	}else{
	$activo = 1;
	}
		// Construye nombres de campos 
	$update = "UPDATE $tabla SET ";
	$query = mssql_query("select top 1 * from $tabla",$link);
	while($field = mssql_fetch_field($query)){
		$cCampo = $field->name;
		if($field->type == "char" or $field->type == "datetime") $com = "'"; else $com = "";
		foreach($_POST as $valor => $x){
			if($cCampo == $valor and $_POST[$cCampo] != '' and $cCampo != 'id_principal' and $cCampo != 'id_unidad'){
				if($field->type == "datetime") {
					$valores = $com.formatofecha($_POST[$cCampo])."$com,";
				}else{
					$valores = $com.$_POST[$cCampo].$com.",";
				}
				$sql .= $cCampo." = ".rtrim($valores);
			}
		}
	}
	// quita ultima coma
	$sql = substr($sql,0,-1);
	$sql = $update.$sql." WHERE id_procedimiento= $nTipoMuestra ";
}
echo $sql;
die();
//$ejecutaQuerys = mssql_query($sql,$link) or die($sql.mssql_get_last_message());


if ($ejecutaQuerys) {
echo "<script language='javascript'> alert('Los datos generales se guardaron correctamente. Por favor continue con el registro de datos clinicos');";
echo "liga('menu_catalogos.php','inicio');</script>";
}



?>